import { Node, Rect, UITransform } from "cc";

export const getAdaptiveRect = (node: Node) => {
    const uiTransform = node.getComponent(UITransform);
    const worldPos = node.getWorldPosition();
    const worldScale = node.getWorldScale();

    const width = uiTransform.width * worldScale.x;
    const height = uiTransform.height * worldScale.y;

    const rectX = worldPos.x - width * uiTransform.anchorX;
    const rectY = worldPos.y - height * uiTransform.anchorY;

    return new Rect(rectX, rectY, width, height);
}

export const deep_clone = (obj) => {
    if (typeof obj !== "object" || obj == null) {
        return obj;
    }
    let result;
    if (obj instanceof Array) {
        result = [];
    } else {
        result = {};
    }
    // 遍历原始对象中的属性
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            result[key] = deep_clone(obj[key]);
        }
    }
    return result;
};